var app = getApp();
const CryptoJS = require('aes_util.js') //参数加密
const httpRequest = require('http_utils.js')

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

var isEmptyObject = function (obj) {
  for (var name in obj) {
    return false;//返回false，不为空对象
  }
  return true;//返回true，为空对象
};

var get = function (url, params, loading = true) {
  return new Promise(function (resolve, reject) {
    if (params) {
      let paramsArray = [];
      //拼接参数
      Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
      if (url.search(/\?/) === -1) {
        url += '?' + paramsArray.join('&')
      } else {
        url += '&' + paramsArray.join('&')
      }
    }
    if (loading) {
      wx.showLoading({
        title: '加载中',
      })
    }

    var header = {
      'content-type': "application/x-www-form-urlencoded",
      'Authorization': "",
      'mobile': '0',
      'UserName': "",
      'Cache-Control': 'max-age=2000000, no-cache'
    }
    wx.getStorage({
      key: 'UserInfo',
      success: function (res) {
        // console.log(JSON.stringify(res))
        if (res != null) {
          header.Authorization = "BasicAuth " + res.data.Ticket;
          header.UserName = res.data.AppLogin.LoginID;
          wx.request({
            url: httpRequest.host + url ,
            data: CryptoJS.AesEncrypt(params),
            header: header,
            success: function (res) {
              // console.log(res)
              wx.hideLoading()
              resolve(res)
            },
            fail: function () {
              wx.hideLoading()
              resolve(null)
            }
          })
        }
      }
    }) 
  })
}

var post = function (url, params, loading = true) {
  return new Promise(function (resolve, reject) {
    if (loading) {
      wx.showLoading({
        title: '加载中',
      })
    }
    var header = {
      'content-type': "application/x-www-form-urlencoded",
      'Authorization': "",
      'mobile': '0',
      'UserName': "",
      'Cache-Control': 'max-age=2000000, no-cache'
    }

    wx.getStorage({
      key: 'UserInfo',
      success: function (res) {
        // console.log(JSON.stringify(res))
        if (res != null) {
          header.Authorization = "BasicAuth " + res.data.Ticket;
          header.UserName = res.data.AppLogin.LoginID;
          

          wx.request({
            url: httpRequest.host + url,
            data: CryptoJS.AesEncrypt(params),
            method: 'POST',
            header: header,
            success: function (res) {
              if (loading) {
                wx.hideLoading()
              }
              resolve(res)
            },
            fail: function () {
              if (loading) {
                wx.hideLoading()
              }
              resolve(null)
            }
          })
        }
      }
    })
  })
}

module.exports = {
  formatTime: formatTime,
  isEmptyObject: isEmptyObject,
  get: get,
  post: post
}
